package eecs.util;
//Class: EECS 285
//Project: 4 (Virtual Markerboard)
//Due Date: Monday December 6th, 2010

import java.awt.Component;

import javax.swing.JOptionPane;

import sun.reflect.Reflection;

/**
 * 
 * A custom of class of my reusable utilities - adapted for this project.
 * 
 * @author Vidal Borromeo
 */
public class Utility
{
  private static boolean DEBUG_MODE = eecs285.proj4.gui.Markerboard.DEBUG_MODE;
  private static boolean SHOW_STACKTRACE = eecs285.proj4.gui.Markerboard.SHOW_STACKTRACE;

  // ---------------------------------------------------------------------------

  /**
   * 
   * Sets the current debug output mode.
   * 
   * @param mode
   *          <code>true</code> -> show debug statements, <code>false</code> ->
   *          hide debug statements.
   * @author Vidal Borromeo
   */
  synchronized public static void setDebugMode(boolean mode)
  {
    DEBUG_MODE = mode;
  }

  // ---------------------------------------------------------------------------

  /**
   * 
   * Sets the current stack trace output mode. If debug mode is false, then
   * stack traces are not shown.
   * 
   * @param mode
   *          <code>true</code> -> show stack traces, <code>false</code> -> hide
   *          stack traces.
   * @author Vidal Borromeo
   */
  synchronized public static void setStackTraceMode(boolean mode)
  {
    SHOW_STACKTRACE = mode;
  }

  // ---------------------------------------------------------------------------

  /**
   * 
   * Gets the current debug output mode.
   * 
   * @return <code>true</code> -> show debug statements, <code>false</code> ->
   *         hide debug statements.
   * @author Vidal Borromeo
   */
  synchronized public static boolean getDebugMode()
  {
    return DEBUG_MODE;
  }

  // ---------------------------------------------------------------------------

  /**
   * 
   * Gets the current stack trace output mode. If debug mode is false, then
   * stack traces are not shown.
   * 
   * @return <code>true</code> -> show stack trace, <code>false</code> -> hide
   *         stack trace.
   * @author Vidal Borromeo
   */
  synchronized public static boolean getStackTraceMode()
  {
    return SHOW_STACKTRACE;
  }

  // ---------------------------------------------------------------------------

  /**
   * 
   * A custom method for printing stacktrace outputs during debugging. The stack
   * trace is only printed if DEBUG_MODE and SHOW_STACKTRACE are true.
   * <ul>
   * <li>To Import: <code>import static eecs.util.Utility.stackTrace;</code>
   * <li>To Use: <code>stackTrace(someException);</code>
   * </ul>
   * 
   * @param e
   *          The exception.
   * @author Vidal Borromeo
   */
  synchronized public static void stackTrace(Exception e)
  {
    if( !DEBUG_MODE || !SHOW_STACKTRACE )
      return;

    synchronized( System.out )
    {
      e.printStackTrace();
    }
  }// debugTrace

  // ---------------------------------------------------------------------------

  /**
   * 
   * A custom method for debug outputs. The supplied output is prepended with
   * the name of the class for easier identification during debugging.
   * <ul>
   * <li>To Import: <code>import static eecs.util.Utility.debug;</code>
   * <li>To Use: <code>debug(message);</code>
   * </ul>
   * Note: Will be removed in later versions because Reflection is a restricted
   * class.
   * 
   * @param msg
   *          The debug statement.
   * @author Vidal Borromeo
   */
  synchronized public static void debug(String msg)
  {
    if( !DEBUG_MODE )
      return;

    synchronized( System.out )
    {
      System.out.println(Reflection.getCallerClass(2).getSimpleName() + ": "
          + msg);
    }
  }// debug

  // ---------------------------------------------------------------------------

  /**
   * 
   * A custom method for displaying a modal dialog box to the user.
   * 
   * @param parent The parent Swing Component of the dialog to be displayed. 
   * @param msg
   *          The message to display to the user.
   * @param title The text to show on the dialog title bar.
   * @author Vidal Borromeo
   */
  public static void doInfo(Component parent, String msg, String title)
  {
    JOptionPane.showMessageDialog(parent, "Info: " + msg, title,
        JOptionPane.INFORMATION_MESSAGE);

  }// doInfo

  // ---------------------------------------------------------------------------

  /**
   * 
   * A custom method for displaying a modal dialog box to the user.
   * 
   * @param parent The parent Swing Component of the dialog to be displayed. 
   * @param msg
   *          The message to display to the user.
   * @param title The text to show on the dialog title bar.
   * @author Vidal Borromeo
   */
  public static void doWarning(Component parent, String msg, String title)
  {
    JOptionPane.showMessageDialog(parent, "Warning: " + msg, title,
        JOptionPane.WARNING_MESSAGE);
  }// doWarning
  
  // ---------------------------------------------------------------------------

  /**
   * 
   * A custom method for displaying a modal dialog box to the user.
   * 
   * @param parent The parent Swing Component of the dialog to be displayed. 
   * @param msg
   *          The message to display to the user.
   * @param title The text to show on the dialog title bar.
   * @author Vidal Borromeo
   */
  public static void doError(Component parent, String msg, String title)
  {
    JOptionPane.showMessageDialog(parent, "Error: " + msg, title,
        JOptionPane.ERROR_MESSAGE);

  }// doError

}// Utility
